package com.example.controller;




import com.example.dao.OrderDao;
import com.example.dao.ShenqingDao;
import com.example.pojo.Order;
import com.example.pojo.Shenqing;
import com.example.util.ExcelUtils;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Order)表控制层
 *
 * @author makejava
 */
@Controller
public class OrderController {
    @Resource
    OrderDao orderDao;

    @Resource
    ShenqingDao shenqingDao;

    /**
     * 跳转到列表页面
     *
     * @return
     */
    @RequestMapping("manage/orderList")
    public String orderList() {
        return "orderList";
    }

    /**
     * 跳转到添加页面
     *
     * @return
     */
    @RequestMapping("manage/addOrder")
    public String addOrder(Model model) {
        List<Shenqing> list = shenqingDao.queryAllByLimit(null);
        model.addAttribute("list",list);
        return "saveOrder";
    }

    /**
     * 跳转到修改页面
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("manage/editOrder")
    public String editOrder(Integer id, Model model) {
        Order order = orderDao.queryById(id);
        model.addAttribute("orderinfo", order);
        return "saveOrder";
    }

    /**
     * 分页查询
     *
     * @param page  默认第一页
     * @param limit 默认每页显示10条
     * @return
     */
    @RequestMapping("manage/queryOrderList")
    @ResponseBody
    public ResultUtil getCarouseList(Integer page, Integer limit, String zhuanye,String name) {
        if (null == page) { //默认第一页
            page = 1;
        }
        if (null == limit) { //默认每页10条
            limit = 10;
        }
        Order order = new Order();
        order.setStuname(name);
        order.setZhuanye(zhuanye);
        PageHelper.startPage(page, limit, true);
        List<Order> list = orderDao.queryAllByLimit(order);
        PageInfo<Order> pageInfo = new PageInfo<Order>(list);  //使用mybatis分页插件
        ResultUtil resultUtil = new ResultUtil();
        resultUtil.setCode(0);  //设置返回状态0为成功
        resultUtil.setCount(pageInfo.getTotal());  //获取总记录数目 类似count(*)
        resultUtil.setData(pageInfo.getList());    //获取当前查询出来的集合
        return resultUtil;
    }

    /**
     * 插入记录
     */
    @RequestMapping("manage/saveOrder")
    @ResponseBody
    public ResultUtil saveOrder(Order order, HttpSession session) {
       
        try {
            orderDao.insert(order);
            return ResultUtil.ok("添加资助信息成功");
        } catch (Exception e) {
            return ResultUtil.error("添加资助信息出错,稍后再试！");
        }
    }

    /**
     * 更新记录
     */
    @RequestMapping("manage/updateOrder")
    @ResponseBody
    public ResultUtil updateOrder(Order order, HttpSession session) {

        try {
            orderDao.update(order);
            return ResultUtil.ok("修改资助信息成功");
        } catch (Exception e) {
            return ResultUtil.error("修改资助信息出错,稍后再试！");
        }
    }


    /**
     * 根据ID删除
     *
     * @param id
     * @return
     */
    @RequestMapping("manage/deleteOrder")
    @ResponseBody
    public ResultUtil deleteOrderById(Integer id) {
        try {
            orderDao.deleteById(id);
            return ResultUtil.ok("删除资助信息成功");
        } catch (Exception e) {
            return ResultUtil.error("删除资助信息出错,稍后再试！");
        }
    }



    /**
     * 根据ID批量删除
     *
     * @param idsStr
     * @return
     */
    @RequestMapping("manage/deletesOrder")
    @ResponseBody
    public ResultUtil deletesOrder(String idsStr) {
        try {
            if (!StringUtils.isBlank(idsStr)) {
                String[] ids = idsStr.split(",");
                for (String id : ids) {
                    orderDao.deleteById(Integer.parseInt(id));
                }
            }
            return ResultUtil.ok("批量删除资助信息成功");
        } catch (Exception e) {
            return ResultUtil.error("删除资助信息出错,稍后再试！");
        }
    }



    /**
     * easyexcel导出Excel到web
     *
     * @param response
     * @return
     */
    @GetMapping("manage/export2Web")
    @ResponseBody
    public void export2Web(HttpServletResponse response) {

            List<Order> orders = orderDao.queryAllByLimit(null);
        try {
            ExcelUtils.export2Web(response, "资助信息表", "资助信息表", Order.class, orders);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

